package com.hboxs.asl.wechat.service.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.service.impl.BaseServiceImpl;
import com.hboxs.asl.wechat.WechatConfig;
import com.hboxs.asl.wechat.dao.WxConfigDao;
import com.hboxs.asl.wechat.entity.WxConfig;
import com.hboxs.asl.wechat.service.WxConfigService;
import com.hboxs.asl.wechat.utils.WechatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by study on 7/4/2015.
 */
@Service("wxConfigServiceImpl")
public class WxConfigServiceImpl extends BaseServiceImpl<WxConfig, Long> implements WxConfigService {
    @Resource(name = "accessTokenDaoImpl")
    private WxConfigDao wxConfigDao;

    @Resource(name = "accessTokenDaoImpl")
    public void setBaseDao(WxConfigDao wxConfigDao) {
        super.setBaseDao(wxConfigDao);
    }

    @Override
    @Transactional(readOnly = true)
    public String latestAccessToken() {
        List<Filter> filters = new ArrayList<>();
        filters.add(Filter.eq("configKey", WxConfig.KEY_ACCESS_TOKEN));
        List<WxConfig> wxConfigs = wxConfigDao.findList(null, 1, filters, null);

        WxConfig wxConfig;
        if(wxConfigs == null || wxConfigs.size() == 0) {
            wxConfig = WechatUtils.accessToken(
                    WechatConfig.APP_ID,
                    WechatConfig.APP_SECRET
            );
            wxConfigDao.persist(wxConfig);
        } else {
            wxConfig = wxConfigs.get(0);
            long expires = (System.currentTimeMillis() - wxConfig.getModifyDate().getTime()) / 1000;
            if(expires >= wxConfig.getExpiresIn()){
                WxConfig newWxConfig =  WechatUtils.accessToken(
                        WechatConfig.APP_ID,
                        WechatConfig.APP_SECRET
                );
                wxConfig.setConfigValue(newWxConfig.getConfigValue());
                wxConfig.setExpiresIn(newWxConfig.getExpiresIn());
                wxConfig.setModifyDate(new Date());
                wxConfigDao.merge(wxConfig);
            }
        }

        return wxConfig.getConfigValue();
    }

    @Override
    @Transactional(readOnly = true)
    public String latestJsapiTicket() {
        List<Filter> filters = new ArrayList<>();
        filters.add(Filter.eq("configKey", WxConfig.KEY_JSAPI_TICKET));
        List<WxConfig> wxConfigs = wxConfigDao.findList(null, 1, filters,null);

        WxConfig wxConfig;
        if(wxConfigs == null || wxConfigs.size() == 0) {
            wxConfig = WechatUtils.jsapiTicket(latestAccessToken());

            if(wxConfig.getConfigValue() != null) wxConfigDao.persist(wxConfig);
        } else {
            wxConfig = wxConfigs.get(0);
            long expires = (System.currentTimeMillis() - wxConfig.getModifyDate().getTime()) / 1000;
            if(expires >= wxConfig.getExpiresIn()){
                WxConfig newWxConfig = WechatUtils.jsapiTicket(latestAccessToken());
                wxConfig.setConfigValue(newWxConfig.getConfigValue());
                wxConfig.setExpiresIn(newWxConfig.getExpiresIn());
                wxConfig.setModifyDate(new Date());

                if(wxConfig.getConfigValue() != null) wxConfigDao.merge(wxConfig);
            }
        }

        return wxConfig.getConfigValue();
    }
}
